package com.xj.qq.domain;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class SQLuser extends SQLiteOpenHelper {

    private static int VERSION=1;
    private static  String DB_NAME="SQLuser.db";
    private static SQLuser busDB;

    public SQLuser(Context context) {
        super(context, DB_NAME,null, VERSION);
    }

    /**
     *
     * 获取数据库实例化
     */
    public static SQLuser getInstance(Context context){
        if(busDB==null){
            busDB=new SQLuser(context);
            return busDB;
        }else{
            return  busDB;
        }
    }
    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE Info(_id INTEGER PRIMARY KEY AUTOINCREMENT," +
                "name TEXT,password TEXT)");
}

    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        if(oldVersion==1&&newVersion==2){
            db.execSQL("ALTER TABLE register ADD phone TEXT;");
        }
    }

    /**
     * 查询表
     */
    public Cursor select(String where,String orderBy){
        StringBuilder buf=new StringBuilder("SELECT * FROM " + "Info");
        if(where!=null){
            buf.append(" WHERE ");
            buf.append(where);
        }
        if(orderBy!=null){
            buf.append(" ORDER BY ");
            buf.append(orderBy);
        }
        Log.e("buf",buf.toString());
        return (getWritableDatabase().rawQuery(buf.toString(),null));
    }

    public void insert(String name,String password){
        ContentValues cv=new ContentValues();
        cv.put("name",name);
        cv.put("password",password);
        getReadableDatabase().insert("Info","name",cv);

    }

    public void del(long id){
        getReadableDatabase().delete("Info","_id=?",new String[]{Integer.valueOf(String.valueOf(id))+""});
    }
    public String getId(Cursor c){
        return c.getString(0);
    }
    public String getName(Cursor c){
        return c.getString(1);
    }
    public String getPassword(Cursor c){
        return c.getString(2);
    }
}
